package leetcode;

public class LeetcodeOffer67 {

    public int strToInt(String str) {
        if (str == null) {
            throw new NumberFormatException("null");
        }

        int result = 0;
        boolean negative = false;
        int i =0, len = str.length();
        int limit = -Integer.MAX_VALUE;
        int multmin;
        int digit;

        if (len > 0) {
            char firstChar = str.charAt(0);
            if (firstChar < '0') {
                if (firstChar == '-') {
                    negative = true;
                    limit = Integer.MAX_VALUE;
                }else if (firstChar != '+') {
                    throw new NumberFormatException(str);
                }
                if (len == 1) {
                    throw new NumberFormatException(str);
                }
                i++;
            }
        }
        multmin = limit / 10;
        while (i < len) {
            char c = str.charAt(i++);
            digit = Character.digit(c, 10);
            if (digit < 0) {
                throw new NumberFormatException(str);
            }
        }
        return negative ? result : -result;
    }

    public static void main(String[] args) {
        int toInt = new LeetcodeOffer67().strToInt("-123");
        System.out.println(toInt);


        int parseInt = Integer.parseInt("-123");
        System.out.println(parseInt);
    }
}
